home *** CD-ROM | disk | FTP | other *** search
- Path: dispatch.news.demon.net!demon!dekard.demon.co.uk
- From: mat@dekard.demon.co.uk (Matt at Home)
- Newsgroups: comp.lang.c++
- Subject: Exceptions *NOT* being handled properly?!?
- Date: Sun, 07 Apr 1996 18:12:44 GMT
- Organization: Information Services, The University of North London.
- Message-ID: <31680560.1181640@news.demon.co.uk>
- NNTP-Posting-Host: dekard.demon.co.uk
- X-NNTP-Posting-Host: dekard.demon.co.uk
- X-Newsreader: Forte Agent .99e/32.194
-
- Compiler: gnu g++ v2.7.2 (i386-univel-sysv4.2MP)
- Environment: UnixWare 2.03 Application Server (16Mb Ram,486DX2-80)
-
- Problem: Exceptions do not seem to be handled correctly (according to
- FAQ 261 of Cline & Lomow)
-
- Given the following code
-
- class X {};
- class X2 : public X {}; // X2 is derived from X
- ....
- try
- {
- throw X2(); // throw the derived class
- }
- catch( const X2& e ) // catch the derived class
- {
- cerr << "Caught exception: Type X2" << endl;
- }
- catch( const X& e ) // catch the base class
- {
- cerr << "Caught exception: Type X" << endl;
- }
- catch( ... ) // catch anything left over
- {
- cerr << "Caught exception: Unknown type" << endl;
- };
- ....
-
- Running this code correctly gives the output
-
- "Caught exception: Type X2"
-
- However if we modify to remove the catch-block for X2 giving :-
-
- try
- {
- throw X2(); // throw the derived class
- }
- catch( const X& e ) // catch the base class
- {
- cerr << "Caught exception: Type X" << endl;
- }
- catch( ... ) // catch anything left over
- {
- cerr << "Caught exception: Unknown type" << endl;
- };
- ....
-
- According to common sense AND to Cline & Lomow FAQ261, I expected :-
-
- "Caught exception: Type X"
-
- Because X2 is a class derived from X ( i.e. we are catching an
- exception through the base class, Cline & Lomows xmsg)
-
- But instead I get :-
-
- "Caught exception: Unknown type"
-
- Can someone tell me :-
-
- 1. What the correct behaviour is?
- 2. Is there something wrong with the code I have written (I have
- similar but *more functional* code which failes the same way in a
- project)?
- 3. Is there something wrong with the g++ compiler?
- 4. If there is, is it likely to be fixed?
-
- Regards,
-
- "Confused"
-
- ----
- Matt Mower - Information Service Team
- M19d. The University of North London.
-